/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.fujiyuu75.sequent.sample;

import com.fujiyuu75.sequent.Animation;
import com.fujiyuu75.sequent.Sequent;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

/**
 * 不同动画效果的演示页面
 */
public class MainAbility extends Ability {

    String[] menus = {"bounceIn", "fadeIn", "fadeInDown", "fadeInUp", "fadeInLeft", "fadeInRight", "rotateIn"};
    Animation[] animations = {Animation.BOUNCE_IN, Animation.FADE_IN, Animation.FADE_IN_DOWN, Animation.FADE_IN_UP,
            Animation.FADE_IN_LEFT, Animation.FADE_IN_RIGHT, Animation.ROTATE_IN};

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_main);
        DirectionalLayout layout = (DirectionalLayout) findComponentById(ResourceTable.Id_layout);
        Button btnMenu = (Button) findComponentById(ResourceTable.Id_menu);

        btnMenu.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                Menu.getInstance(getContext(), component, menus)
                        .setItemTextStyle(20, 0xff000000, false)
                        .isDarkThem()
                        .setMenuPadding(16, 10, 16, 10)
                        .setItemClickListener(new Menu.ItemClickListener() {
                            @Override
                            public void onItemClick(Component item, int position, String text) {
                                Sequent.origin((ComponentContainer) findComponentById(ResourceTable.Id_layout)).anim(animations[position]).start();
                            }
                        }).show();
            }
        });
        findComponentById(ResourceTable.Id_button).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                startAbility(MainAbility.this, SampleAbility.class);
            }
        });

        new EventHandler(EventRunner.getMainEventRunner()).postTask(new Runnable() {
            @Override
            public void run() {
                layout.setVisibility(Component.VISIBLE);
                Sequent.origin((ComponentContainer) findComponentById(ResourceTable.Id_layout)).anim(Animation.BOUNCE_IN).start();
            }
        }, 100);
    }

    /**
     * 开启另一个Ability
     *
     * @param aAbility 当前ability对象
     * @param bAbility 要开启的Ability的class
     */
    public void startAbility(Ability aAbility, Class bAbility) {
        Intent secondIntent = new Intent();
        Operation operation = new Intent.OperationBuilder()
                .withBundleName(aAbility.getBundleName())
                .withAbilityName(bAbility.getName())
                .build();
        secondIntent.setOperation(operation);
        aAbility.startAbility(secondIntent);
    }

}
